﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChairtyBank.DAL.PreAccept;

namespace ChairtyBank
{
    public partial class PreAcceptForm : Form
    {

        
        public Int32 currentClient = -1;
        public Int32 currentSearch = -1;
        public Int32 currentTamweel = -1;
        public string CurrentAkhesaye = "";
        public Int32 CurrentDamn = -1;
        List<DamnEntity> DamnEmptyList = new List<DamnEntity>();

        public void ClearClient()
        {
            txtID.Text = "";
            txtName.Text = "";
            comboGender.Text = "";
            txtPhone.Text = "";
            txtMobile.Text = "";
            txtAddress.Text = "";
            txtRegion.Text = "";
        }

        public void ClearDamn()
        {
            txtD_Name.Text = "";
            txtD_Relation.Text = "";
            txtD_Job.Text = "";
            txtD_Phone.Text = "";
            txtD_Mobile.Text = "";
            txtD_Address.Text = "";
            txtD_CardID.Text = "";
            txtD_SalaryPlace.Text = "";
        }

        public void ClearSearch()
        {
            txtS_ID.Text = "";
            ComboKrarSearch.Text = "";
            txtS_Reason.Text = "";
            comboAkhesaye.Text = "";
        }

        public void ClearTamweel()
        {
            txtT_ID.Text = "";
            ComboKrarTamweel.Text = "";
            txtT_Reason.Text = "";
            ComboT_Akhesaye.Text = "";
        }

        public void ClearRequest()
        {
            txtRequestID.Text = "";
            txtL_monthlyValue.Text = "";
            txtL_NoOfMonths.Text = "";
            txtL_Value.Text = "";
            comboL_Type.Text = "";
        }

        public ClientEntity GetClient()
        {
            ClientEntity c = new ClientEntity();
            c.C_Name = txtName.Text;
            c.C_Gender = comboGender.Text;
            c.C_Phone = txtPhone.Text;
            c.C_Mobile = txtMobile.Text;
            c.C_Address = txtAddress.Text;
            c.C_Region = txtRegion.Text;
            c.C_Id = Convert.ToInt32(txtID.Text);

            return c;
        }

        public DamnEntity GetDamn()
        {
            DamnEntity c = new DamnEntity();
            c.D_Address = txtD_Address.Text;
            c.D_IdNum = txtD_CardID.Text;
            c.D_Job = txtD_Job.Text;
            c.D_Moblie = txtD_Mobile.Text;
            c.D_Name = txtD_Name.Text;
            c.D_Phone = txtD_Phone.Text;
            c.D_Relation = txtD_Relation.Text;
            c.D_SalaryPlace = txtD_SalaryPlace.Text;

            return c;


        }

        public SearchEntity GetSearch()
        {
            SearchEntity s = new SearchEntity();
            s.Search_Id = Convert.ToInt32(txtS_ID.Text);
            s.S_Desision = ComboKrarSearch.Text;
            s.S_Reason = txtS_Reason.Text;
            s.Search_Akhesaye = comboAkhesaye.Text;

            return s;
        }

        public TamweelEntity GetTamweel()
        {
            TamweelEntity t = new TamweelEntity();
            t.T_Id = Convert.ToInt32(txtT_ID.Text);
            t.T_Decission = ComboKrarTamweel.Text;
            t.T_Akhesaye = ComboT_Akhesaye.Text;
            t.T_Reason = txtT_Reason.Text;

            return t;
        }

        public AkhesayeEntity GetAkhesaye()
        {
            AkhesayeEntity s = new AkhesayeEntity();
            s.Name = comboAkhesaye.Text;
            return s;
        }

        public AkhesayeEntity GetAkhesayeT()
        {
            AkhesayeEntity s = new AkhesayeEntity();
            s.Name = ComboT_Akhesaye.Text;
            return s;
        }

        public RequestEntity GetRquest()
        {
            RequestEntity r = new RequestEntity();
            r.R_Id = Convert.ToInt32(txtRequestID.Text);
            r.Loan_Type = comboL_Type.Text;
            r.Loan_Value = Convert.ToDouble(txtL_Value.Text);
            r.MonthlyPayment = Convert.ToDouble(txtL_monthlyValue.Text);
            r.NomberOfMonths = Convert.ToInt32(txtL_NoOfMonths.Text);
            r.Client_Id = Convert.ToInt32(txtID.Text);

            return r;
        }

        public void PublishClient(ClientEntity c)
        {
            txtID.Text = Convert.ToString(c.C_Id);
            txtName.Text = c.C_Name;
            comboGender.Text = c.C_Gender;
            txtPhone.Text = c.C_Phone;
            txtMobile.Text = Convert.ToString(c.C_Mobile);
            txtAddress.Text = c.C_Address;
            txtRegion.Text = c.C_Region;

        }

        public void PublishDamn(DamnEntity D)
        {
            txtD_Address.Text = D.D_Address;
            txtD_CardID.Text = Convert.ToString(D.D_IdNum);
            txtD_Job.Text = D.D_Job;
            txtD_Mobile.Text = D.D_Moblie;
            txtD_Name.Text = D.D_Name;
            txtD_Phone.Text = D.D_Phone;
            txtD_Relation.Text = D.D_Relation;
            txtD_SalaryPlace.Text = D.D_SalaryPlace;
        }

        public void PublishRequest(RequestEntity a)
        {
            txtRequestID.Text = Convert.ToString(a.R_Id);
            txtL_monthlyValue.Text = Convert.ToString(a.MonthlyPayment);
            txtL_NoOfMonths.Text = Convert.ToString(a.NomberOfMonths);
            comboL_Type.Text = a.Loan_Type;
            txtL_Value.Text = Convert.ToString(a.Loan_Value);
            txtRequestID.Text = Convert.ToString(a.R_Id);
        }

        public void PublishSearch(SearchEntity x)
        {
            comboAkhesaye.Text = x.Search_Akhesaye;
            ComboKrarSearch.Text = x.S_Desision;
            txtS_ID.Text = (x.Search_Id).ToString();
            txtS_Reason.Text = x.S_Reason;
        }

        public void PublishTamweel(TamweelEntity s)
        {
            ComboT_Akhesaye.Text = s.T_Akhesaye;
            ComboKrarTamweel.Text = s.T_Decission;
            txtT_ID.Text = Convert.ToString(s.T_Id);
            txtT_Reason.Text = s.T_Reason;
        }

        public PreAcceptForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadAkhesa2yComboBox();
            LoadComboT_AkhesayeComboBox();

        }

        private void btnC_Clear_Click(object sender, EventArgs e)
        {
            ClearClient();
            ClearRequest();
            ClearDamn();
            ClearSearch();
            ClearTamweel();
            dataGridView.DataSource = DamnEmptyList;
            currentClient = -1;
            CurrentDamn = -1;
            currentSearch = -1;
            currentTamweel = -1;
        }

        private void btnD_Clear_Click(object sender, EventArgs e)
        {
            ClearDamn();
        }

        private void btnS_Clear_Click(object sender, EventArgs e)
        {
            ClearSearch();
        }

        private void btnT_Clear_Click(object sender, EventArgs e)
        {
            ClearTamweel();
        }

        private void btnC_Save_Click(object sender, EventArgs e)
        {
            try
            {
                ClientEntity Cl = GetClient();
                RequestEntity r = GetRquest();
                if ((Cl == null) || (r == null))
                {
                    MessageBox.Show("يجب ادخال جميع البيانات");
                }
                else
                {

                    if (currentClient == -1)
                    {
                        Client.Add(Cl);
                        Request.Add(r);
                        currentClient = Client.GetClientByName(Cl.C_Name)[0].C_Id;
                        MessageBox.Show("تم حفظ البيانات ", "رساله ادارية ", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                    else
                    {
                        Client.Update(Cl);
                        Request.Update(r);
                        MessageBox.Show("تم التعديل", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("خطــــأ" + ex.Message);
            }

        }

        private void btnC_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentClient != -1)
                {
                    DialogResult result = MessageBox.Show("هل تريد حذف هذا العميل ؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (DialogResult.Yes == result)
                    {
                        int c = Convert.ToInt32(txtID.Text);
                        int r = Convert.ToInt32(txtRequestID.Text);

                        Client.Delete(c);
                        Request.Delete(r);
                        Search.Delete(r);
                        Tamweel.Delete(r);
                        Damn.Delete(r);
     
                        ClearClient();
                        ClearRequest();
                        ClearSearch();
                        ClearTamweel();
                        ClearDamn();
                        List<DamnEntity> DamnEmptyList = new List<DamnEntity>();
                        dataGridView.DataSource = DamnEmptyList;

                        MessageBox.Show("تم حذف العميل", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }

                }
                else
                {
                    MessageBox.Show("يجب اختيار العميل أولا", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtName_KeyDown_1(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    List<ClientEntity> c = Client.GetClientByName("%" + txtName.Text + "%");
                    int x = (c[0]).C_Id;
                    List<RequestEntity> R = Request.GetRequestByClientID(x);
                    int a = R[0].R_Id;
                    List<SearchEntity> S = Search.GetSearchByID(a);
                    List<TamweelEntity> T = Tamweel.GetTamweelByID(a);
                    List<DamnEntity> D = Damn.GetDamnByID(a);

                    if (c.Count == 0)
                        MessageBox.Show("هذا العميل غير موجود");
                    else
                    {
                        if (c.Count == 1)
                        {

                            if ((S.Count == 0) && (T.Count == 0) && (D.Count == 0))
                            {
                                PublishClient(c[0]);
                                PublishRequest(R[0]);
                                currentClient = x;
                            }
                            else if ((S.Count == 0) && (T.Count == 0))
                            {
                                PublishClient(c[0]);
                                PublishRequest(R[0]);
                                dataGridView.DataSource = D;
                                currentClient = x;

                            }
                            else if ((T.Count == 0) && (D.Count == 0))
                            {
                                PublishClient(c[0]);
                                PublishRequest(R[0]);
                                PublishSearch(S[0]);
                                currentClient = x;
                                currentSearch = a;

                            }
                            else if ((S.Count == 0) && (D.Count == 0))
                            {
                                PublishClient(c[0]);
                                PublishRequest(R[0]);
                                PublishSearch(S[0]);
                                PublishTamweel(T[0]);
                                currentClient = x;
                                currentTamweel = a;
                            }
                            else if (T.Count == 0)
                            {
                                PublishClient(c[0]);
                                PublishRequest(R[0]);
                                PublishSearch(S[0]);
                                dataGridView.DataSource = D;
                                currentClient = x;
                                currentSearch = a;
                            }
                            else if (S.Count == 0)
                            {
                                PublishClient(c[0]);
                                PublishRequest(R[0]);
                                PublishTamweel(T[0]);
                                dataGridView.DataSource = D;
                                currentClient = x;
                                currentTamweel = a;
                            }
                            else if (D.Count == 0)
                            {
                                PublishClient(c[0]);
                                PublishRequest(R[0]);
                                PublishTamweel(T[0]);
                                PublishSearch(S[0]);
                                currentSearch = a;
                                currentClient = x;
                                currentTamweel = a;
                            }

                            else
                            {
                                PublishClient(c[0]);
                                PublishRequest(R[0]);
                                PublishSearch(S[0]);
                                PublishTamweel(T[0]);
                                dataGridView.DataSource = D;
                                currentClient = x;
                                currentSearch = a;
                                currentTamweel = a;
                            }
                        }
                        else
                        {
                            gridclient.DataSource = c;
                            gridclient.Show();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show((" خطأ في البيانات"), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void gridclient_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                List<ClientEntity> list = (List<ClientEntity>)gridclient.DataSource;
                int x = (list[e.RowIndex]).C_Id;
                List<RequestEntity> R = Request.GetRequestByClientID(x);
                int a = R[0].R_Id;
                List<SearchEntity> S = Search.GetSearchByID(a);
                List<TamweelEntity> T = Tamweel.GetTamweelByID(a);
                List<DamnEntity> D = Damn.GetDamnByID(a);

                if ((S.Count == 0) && (T.Count == 0) && (D.Count ==0))
                {
                    PublishClient(list[e.RowIndex]);
                    PublishRequest(R[0]);
                    currentClient = x;
                }

                else if ((S.Count == 0) && (T.Count == 0))
                {
                    PublishClient(list[e.RowIndex]);
                    PublishRequest(R[0]);
                    dataGridView.DataSource = D;
                    currentClient = x;

                }
                else if ((T.Count == 0) && (D.Count == 0))
                {
                    PublishClient(list[e.RowIndex]);
                    PublishRequest(R[0]);
                    PublishSearch(S[0]);
                    currentClient = x;
                    currentSearch = a;

                }
                else if ((S.Count == 0) && (D.Count == 0))
                {
                    PublishClient(list[e.RowIndex]);
                    PublishRequest(R[0]);
                    PublishSearch(S[0]);
                    PublishTamweel(T[0]);
                    currentClient = x;
                    currentTamweel = a;
                }
                else if (T.Count == 0)
                {
                    PublishClient(list[e.RowIndex]);
                    PublishRequest(R[0]);
                    PublishSearch(S[0]);
                    dataGridView.DataSource = D;
                    currentClient = x;
                    currentSearch = a;
                }
                else if (S.Count == 0)
                {
                    PublishClient(list[e.RowIndex]);
                    PublishRequest(R[0]);
                    PublishTamweel(T[0]);
                    dataGridView.DataSource = D;
                    currentClient = x;
                    currentTamweel = a;
                }
                else if (D.Count == 0)
                {
                    PublishClient(list[e.RowIndex]);
                    PublishRequest(R[0]);
                    PublishTamweel(T[0]);
                    PublishSearch(S[0]);
                    currentSearch = a;
                    currentClient = x;
                    currentTamweel = a;
                }

                else
                {
                    PublishClient(list[e.RowIndex]);
                    PublishRequest(R[0]);
                    PublishSearch(S[0]);
                    PublishTamweel(T[0]);
                    dataGridView.DataSource = D;
                    currentClient = x;
                    currentSearch = a;
                    currentTamweel = a;
                }

                gridclient.Hide();
            }
            else
            {
                MessageBox.Show("خطـــأ .... يجب تحديد العميل ");
            }
        }

        private void comboGender_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    ClientEntity Cl = GetClient();
                    RequestEntity r = GetRquest();
                    if ((Cl == null) || (r == null))
                    {
                        MessageBox.Show("يجب ادخال جميع البيانات");
                    }
                    else
                    {

                        if (currentClient == -1)
                        {
                            Client.Add(Cl);
                            Request.Add(r);
                            currentClient = Client.GetClientByName(Cl.C_Name)[0].C_Id;
                            MessageBox.Show("تم حفظ البيانات ", "رساله ادارية ", MessageBoxButtons.OK, MessageBoxIcon.None);
                        }
                        else
                        {
                            Client.Update(Cl);
                            Request.Update(r);
                            MessageBox.Show("تم التعديل", "Message", MessageBoxButtons.OK, MessageBoxIcon.None);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void LoadAkhesa2yComboBox()
        {
            comboAkhesaye.Items.Clear();
            List<AkhesayeEntity> AList = Akhesaye.GetAkhesaye();
            foreach (AkhesayeEntity A in AList)
            {
                comboAkhesaye.Items.Add(A.Name);
            }
        }

        private void BtnAddAkhesaye_Click(object sender, EventArgs e)
        {
            List<AkhesayeEntity> AkhesayeList = Akhesaye.GetAkhesaye();
            try
            {
                if (comboAkhesaye.Text == "")
                {
                    MessageBox.Show("يجب ادخال اسم للأخصائى", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (comboAkhesaye.Text == AkhesayeList[0].Name)
                {
                    MessageBox.Show("هذا الأخصائى موجود من قبل","رسالة ادارية",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
                else
                {
                    Akhesaye.Add(GetAkhesaye());
                    comboAkhesaye.Text = "";
                    LoadAkhesa2yComboBox();
                    LoadComboT_AkhesayeComboBox();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboAkhesaye.Text == "")
                {
                    MessageBox.Show("يجب تحديد اسم للأخصائى", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    DialogResult Result = MessageBox.Show(" هل تريد حذف هذا الأخصائى ؟ ", "رسالة ادارية ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (DialogResult.Yes == Result)
                    {
                        Akhesaye.Delete(comboAkhesaye.SelectedItem.ToString());
                        MessageBox.Show("تم الحذف ", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                        comboAkhesaye.Text = "";
                        LoadAkhesa2yComboBox();
                        LoadComboT_AkhesayeComboBox();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LoadComboT_AkhesayeComboBox()
        {
            ComboT_Akhesaye.Items.Clear();
            List<AkhesayeEntity> AList = Akhesaye.GetAkhesaye();
            foreach (AkhesayeEntity A in AList)
            {
                ComboT_Akhesaye.Items.Add(A.Name);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            List<AkhesayeEntity> ListAkhesayeTamweel = Akhesaye.GetAkhesaye();
            try
            {
                if (ComboT_Akhesaye.Text == "")
                {
                    MessageBox.Show("يجب ادخال اسم للأخصائى", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (ComboT_Akhesaye.Text == ListAkhesayeTamweel[0].Name)
                {
                    MessageBox.Show("هذا الأخصائى موجود من قبل", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Akhesaye.Add(GetAkhesayeT());
                    ComboT_Akhesaye.Text = "";
                    LoadComboT_AkhesayeComboBox();
                    LoadAkhesa2yComboBox();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (ComboT_Akhesaye.Text == "")
                {
                    MessageBox.Show("يجب تحديد اسم للأخصائى", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    DialogResult Result = MessageBox.Show(" هل تريد حذف هذا الأخصائى ؟ ", "رسالة ادارية ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (DialogResult.Yes == Result)
                    {
                        Akhesaye.Delete(ComboT_Akhesaye.SelectedItem.ToString());
                        ComboT_Akhesaye.Text = "";
                        LoadComboT_AkhesayeComboBox();
                        LoadAkhesa2yComboBox();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnS_Save_Click(object sender, EventArgs e)
        {
            try
            {
              SearchEntity x = GetSearch();
              AkhesayeEntity k = GetAkhesaye();
              x.Search_Id = Convert.ToInt32(txtRequestID.Text);
              x.Search_Akhesaye = comboAkhesaye.Text;

                if  (currentClient != -1)
                {
                    if (currentSearch == -1)
                    {
                        Search.Add(x);
                        List<SearchEntity> a = Search.GetSearchByID(Convert.ToInt32(txtRequestID.Text));
                        currentSearch = a[0].Search_Id;
                        MessageBox.Show("تم الحفظ ", "رساله ادارية ", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                    else
                    {
                        x.Search_Id = currentSearch;
                        Search.Update(GetSearch());
                        MessageBox.Show("تم التعديل ", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                }
                else
                {
                    MessageBox.Show("يجب تحديد العميل اولاً ", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnS_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentSearch != -1)
                {
                    DialogResult Result = MessageBox.Show(" هل تريد حذف البحث ؟؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (DialogResult.Yes == Result)
                    {

                        Int32 c = Int32.Parse(txtS_ID.Text);

                        if (currentSearch != -1)
                        {
                            Search.Delete(c);
                            MessageBox.Show(" تم حذف البحث ", "رساله اداريه ", MessageBoxButtons.OK, MessageBoxIcon.None);
                            ClearSearch();
                            currentSearch = -1;
                        }
                        else
                        {
                            if (txtRequestID.Text == "")
                            {
                                MessageBox.Show("يجب تحديد العميل اولاً");
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message),"رسالة ادارية",MessageBoxButtons.OK,MessageBoxIcon.Error) ;
            }  

          

        }

        private void txtT_ID_TextChanged(object sender, EventArgs e)
        {
            txtT_ID.Text = txtRequestID.Text;
        }

        private void btnT_Save_Click(object sender, EventArgs e)
        {
            try
            {
            TamweelEntity x = GetTamweel();
            AkhesayeEntity k = GetAkhesayeT();
            x.T_Id = Convert.ToInt32(txtRequestID.Text);
            x.T_Akhesaye = ComboT_Akhesaye.Text;

                if (currentClient != -1)
                {
                    if (currentTamweel == -1)
                    {
                        Tamweel.Add(x);
                        List<TamweelEntity> a = Tamweel.GetTamweelByID(Convert.ToInt32(txtRequestID.Text));
                        currentTamweel = a[0].T_Id;
                        MessageBox.Show("تم الحفظ ", "رسالة ادارية ", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                    else
                    {
                        x.T_Id = currentTamweel;
                        Tamweel.Update(GetTamweel());
                        MessageBox.Show("تم التعديل ", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                }
                else
                {
                    MessageBox.Show("يجب تحديد العميل اولاً ", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnT_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentTamweel != -1)
                {
                    DialogResult Result = MessageBox.Show("هل تريد الحذف ....?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (DialogResult.Yes == Result)
                    {
                        Int32 c = Convert.ToInt32(txtT_ID.Text);
                        Tamweel.Delete(c);
                        MessageBox.Show(" تم حذف البحث ", "", MessageBoxButtons.OK, MessageBoxIcon.None);
                        ClearTamweel();
                        currentTamweel = -1;
                    }
                    else
                    {
                        if (txtRequestID.Text == "")
                        {
                            MessageBox.Show("يجب تحديد العميل اولاً");
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message),"رسالة ادارية",MessageBoxButtons.OK,MessageBoxIcon.Error) ;
            }

        }

        private void btnD_Save_Click(object sender, EventArgs e)
        {
            try
            {
                DamnEntity D = GetDamn();
                D.R_Id = Convert.ToInt32(txtRequestID.Text);
                D.C_Id = Convert.ToInt32(txtID.Text);

                if (txtRequestID.Text == "")
                {
                    MessageBox.Show("يجب نحديد رقم الطلب  ","رسالة ادارية",MessageBoxButtons.OK,MessageBoxIcon.Error );
                }
                else if ((D.D_Address == "") || (txtD_CardID.Text == "") || D.D_Job == "" || (txtD_Mobile.Text == "") || D.D_Name == "" || txtD_Phone.Text == "" || D.D_Relation == "" || D.D_SalaryPlace == "" )
                {
                    MessageBox.Show("يجب ادخال جميع البيانات","رسالة ادارية",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
                else
                {
                    if (CurrentDamn == -1)
                    {
                        Damn.Add(D);
                        List<DamnEntity> x = Damn.GetDamnByID(Convert.ToInt32(txtRequestID.Text));
                        dataGridView.DataSource = x;
                        CurrentDamn = Damn.GetDamnByName(D.D_Name)[0].D_Id;
                        // MessageBox.Show("تم حفظ البيانات", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }

                    else
                    {
                        DialogResult result = MessageBox.Show("هل تريد تعديل بيانات هذا الضامن ؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (DialogResult.Yes == result)
                        {
                            D.D_Id = CurrentDamn;
                            Damn.Update(D);
                            List<DamnEntity> x = Damn.GetDamnByID(Convert.ToInt32(txtRequestID.Text));
                            dataGridView.DataSource = x;
                            MessageBox.Show("تم تعديل الضامن : " + D.D_Name, "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message),"رسالة ادارية",MessageBoxButtons.OK,MessageBoxIcon.Error) ;
            }

            
        }

        private void btnD_Delete_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (CurrentDamn != -1)
                {
                    DialogResult result = MessageBox.Show(" هل تريد حذف هذا الضامن ؟ ", "رسالة ادارية", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (DialogResult.Yes == result)
                    {
                        Damn.Delete(CurrentDamn);
                        List<DamnEntity> D = Damn.GetDamnByID(Convert.ToInt32(txtRequestID.Text));
                        dataGridView.DataSource = D;
                        CurrentDamn = -1;
                        ClearDamn();
                        //MessageBox.Show("تم الحذف", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                }
                else
                {
                    MessageBox.Show("يجب تحديد الضامن اولا", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message),"رسالة ادارية",MessageBoxButtons.OK,MessageBoxIcon.Error) ;
            }
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void txtL_NoOfMonths_Enter(object sender, EventArgs e)
        {
            try
            {
                int x = Convert.ToInt32(txtL_Value.Text) / Convert.ToInt32(txtL_monthlyValue.Text);
                if ((Convert.ToInt32(txtL_Value.Text)) % (Convert.ToInt32(txtL_monthlyValue.Text)) != 0)
                {
                    x++;
                }
                txtL_NoOfMonths.Text = Convert.ToString(x);
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnD_Clear_Click_1(object sender, EventArgs e)
        {
            ClearDamn();
            CurrentDamn = -1;
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.RowIndex != -1)
            {

                List<DamnEntity> listDamn = (List<DamnEntity>)dataGridView.DataSource;
                int x = (listDamn[e.RowIndex]).D_Id;
                PublishDamn(listDamn[e.RowIndex]);
                CurrentDamn = x;
            }
        }

        private void txtS_ID_Click(object sender, EventArgs e)
        {
            txtS_ID.Text = txtRequestID.Text;
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    List<ClientEntity> ClientList = Client.GetClientByID(Convert.ToInt32(txtID.Text));

                    if (txtID.Text == "")
                    {
                        MessageBox.Show("يجب ادخال رقم العميل", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (ClientList.Count == 0)
                    {
                        MessageBox.Show("هذا العميل غير موجود", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        int C_Id = ClientList[0].C_Id;
                        List<RequestEntity> RequestList = Request.GetRequestByClientID(Convert.ToInt32(txtID.Text));
                        int R_Id = RequestList[0].R_Id;
                        List<SearchEntity> SearchList = Search.GetSearchByID(R_Id);
                        List<TamweelEntity> TamweelList = Tamweel.GetTamweelByID(R_Id);
                        List<DamnEntity> DamnList = Damn.GetDamnByID(R_Id);
                        PublishClient(ClientList[0]);
                        PublishRequest(RequestList[0]);
                        PublishSearch(SearchList[0]);
                        PublishTamweel(TamweelList[0]);
                        PublishDamn(DamnList[0]);
                        dataGridView.DataSource = DamnList;
                        currentClient = C_Id;
                        currentSearch = R_Id;
                        currentTamweel = R_Id;
                        CurrentDamn = DamnList[0].D_Id;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(("خطأ" + ex.Message),"رسالة ادارية",MessageBoxButtons.OK,MessageBoxIcon.Error) ;
            }
        }

        private void txtT_ID_Click(object sender, EventArgs e)
        {
            txtT_ID.Text = txtRequestID.Text;
        }

        private void txtD_Mobile_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                try
                {
                    DamnEntity D = GetDamn();
                    D.R_Id = Convert.ToInt32(txtRequestID.Text);
                    D.C_Id = Convert.ToInt32(txtID.Text);


                    if (D == null)
                    {
                        MessageBox.Show("يجب ادخال جميع البيانات");
                    }
                    else
                    {
                        if (CurrentDamn == -1)
                        {
                            Damn.Add(D);
                            List<DamnEntity> x = Damn.GetDamnByID(Convert.ToInt32(txtRequestID.Text));
                            dataGridView.DataSource = x;
                            CurrentDamn = Damn.GetDamnByName(D.D_Name)[0].D_Id;
                            MessageBox.Show("تم حفظ البيانات", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                        }

                        else
                        {
                            D.D_Id = CurrentDamn;
                            Damn.Update(D);
                            List<DamnEntity> x = Damn.GetDamnByID(Convert.ToInt32(txtRequestID.Text));
                            dataGridView.DataSource = x;
                            MessageBox.Show("تم التعديل", "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.None);
                        }
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void groupBox6_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {

        }

        private void txtL_NoOfMonths_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    ClientEntity Cl = GetClient();
                    RequestEntity r = GetRquest();
                    if ((Cl == null) || (r == null))
                    {
                        MessageBox.Show("يجب ادخال جميع البيانات");
                    }
                    else
                    {

                        if (currentClient == -1)
                        {
                            Client.Add(Cl);
                            Request.Add(r);
                            currentClient = Client.GetClientByName(Cl.C_Name)[0].C_Id;
                            MessageBox.Show("تم حفظ البيانات ", "رساله ادارية ", MessageBoxButtons.OK, MessageBoxIcon.None);
                        }
                        else
                        {
                            Client.Update(Cl);
                            Request.Update(r);
                            MessageBox.Show("تم التعديل", "Message", MessageBoxButtons.OK, MessageBoxIcon.None);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(("خطأ" + ex.Message), "رسالة ادارية", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

    }
}
